Skip to main content

Loops

Loops allow a workflow to repeat an operation or group of operations while a runtime decision indicates that another iteration should occur. Loops are useful when the same workflow logic must execute repeatedly for a collection, batch, polling process, iterative calculation, or other intentional repeated operation. The exact public API for defining workflow loops should be taken from the installed BindAI implementation.

What Is a Loop?

A loop creates repeated execution within a workflow. Conceptually:
The loop decides whether another iteration should execute. After the loop body completes, execution can return to the loop decision.

Why Use Loops?

Loops are useful when the same workflow logic must execute repeatedly. Common examples include:
  • Processing collections
  • Processing batches
  • Polling external systems
  • Waiting for background operations
  • Repeated validation
  • Processing paginated results
  • Performing repeated calculations
  • Incremental synchronization
Without loop behavior, repeated workflow logic would need to be duplicated or manually represented through graph cycles.

Loop Execution Flow

A typical loop follows this pattern:
When the continuation decision is true, the workflow executes the loop body. After the body completes, the workflow evaluates the decision again. When the continuation decision becomes false, execution leaves the loop. The exact mechanics depend on the workflow execution implementation.

Loop State

Loops normally depend on information that changes as execution progresses. For example:
The loop body must make progress toward its terminating state. If the state used to determine continuation never changes, the workflow may continue indefinitely.

Loop Body

The loop body contains the operations performed during one iteration. For example:
A loop body can contain multiple operations. Depending on the workflow, those operations may include:
  • Agents
  • Tools
  • Conditions
  • Knowledge retrieval
  • Memory operations
  • External integrations
  • Validation
  • Data processing
The body should remain focused on the work required for one iteration.

Loop Exit

Every loop should have a clear exit path. Conceptually:
The exit path allows downstream workflow operations to continue after iteration is complete. A loop without a reachable exit can cause an unbounded workflow execution.

Loop Decisions

The continuation decision determines whether another iteration should occur. Conceptually:
The decision can depend on values produced by previous workflow operations. For example, a workflow may continue while:
  • More items remain
  • A task is not complete
  • A validation condition has not been satisfied
  • Additional pages are available
  • A synchronization process still has work
The exact decision API should follow the implemented workflow engine.

Example: Processing a Collection

A common loop pattern processes items one at a time.
Conceptually, the workflow maintains:
The continuation decision checks whether another item remains. For example:
The body processes the current item and advances the position.

Example: Polling an External Service

A loop can repeatedly check the state of an asynchronous operation.
The workflow can retain the current operation status and evaluate it after each polling interval. For production polling workflows, include an appropriate delay or backoff mechanism so that the external service is not queried continuously. A maximum polling duration or timeout can also prevent an external operation from keeping the workflow active indefinitely.

Example: Batch Processing

Loops can process batches incrementally.
The continuation decision determines whether another batch remains. This approach can prevent a large collection from being processed in one oversized workflow operation.

Example: Pagination

A workflow can repeatedly request pages of data.
The workflow can use pagination information returned by the external service to determine whether another iteration is required.

Example: Repeated Validation

A workflow may repeat validation until a desired state is reached.
The loop should have a reliable termination mechanism when the data or external state is unpredictable.

Loops vs Retry

Loops and retries both involve repeated execution, but they solve different problems. Use a loop when repetition is part of the intended workflow behavior. Use retry when an operation fails and should be attempted again according to a reliability policy. A retry should not normally be implemented by wrapping every failure in a general-purpose workflow loop.

Loops with Conditions

Conditions can be used inside a loop.
The loop controls repetition. The condition controls a decision within the iteration. This separation is useful when each iteration contains several possible outcomes.

Loops with Agents

An agent can participate in each loop iteration. For example:
This can be useful for iterative analysis, classification, planning, or processing of multiple items. The workflow should still define clear termination behavior independently of the model’s output.

Loops with Tools

Tools are useful when each iteration performs deterministic work.
Examples include:
  • Processing records
  • Calling an API for each item
  • Writing application data
  • Performing calculations
  • Synchronizing external resources
Tool failures should follow the workflow’s error and retry policies.

Loops with Knowledge

Knowledge retrieval can be performed during repeated processing. For example:
This can be useful when each item requires separate retrieval or contextual analysis. Knowledge remains responsible for retrieval, while the workflow controls iteration.

Loops with Memory

Memory can also participate in iterative workflows. For example:
Memory can preserve information beyond the immediate operation. Workflow state, meanwhile, should carry information required for the current execution process.

Nested Loops

Loops can be combined with other loops. For example:
A common use case is hierarchical data:
Nested loops can be useful, but excessive nesting makes workflows harder to understand and debug. When nested iteration becomes complex, consider splitting the process into smaller workflow stages.

Avoiding Infinite Loops

Every loop needs a reliable path toward termination. For example:
The loop body changes the state used by the continuation decision. Eventually:
If the state never changes, the continuation decision may remain true indefinitely. Good loop design therefore requires:
  1. A clear termination condition
  2. State that changes during execution
  3. Measurable progress toward termination
  4. Appropriate safeguards for long-running operations

Maximum Iterations

For loops that depend on external systems or unpredictable data, a maximum iteration limit can provide an additional safety mechanism. Conceptually:
This prevents unexpected state from causing unbounded workflow execution. The exact configuration mechanism should follow the implemented BindAI workflow API.

Timeouts and Cancellation

Long-running loops should also consider execution limits. Conceptually:
These safeguards are particularly important for:
  • External polling
  • Large collections
  • Untrusted input
  • Slow integrations
  • Long-running agent operations
The exact timeout and cancellation APIs depend on the workflow implementation.

Error Handling

A failure inside a loop body should follow the workflow’s configured error-handling behavior. Possible strategies include:
The correct behavior depends on the operation and application requirements. For transient failures, use retry policies rather than treating every retry as a new business-level loop iteration.

Loop Progress

A useful loop should make measurable progress. For example:
Progress can be represented by:
  • An item position
  • A page number
  • A batch identifier
  • A task status
  • A completion flag
  • Remaining work
Making progress explicit helps prevent accidental non-termination.

Testing Loops

Loop behavior should be tested across several scenarios.

Zero iterations

The continuation decision is initially false.
The loop body should not execute.

One iteration

The decision is initially true but becomes false after one iteration.

Multiple iterations

The decision remains true for several iterations before becoming false.

Non-terminating state

The continuation decision never becomes false. The workflow should have an appropriate safeguard such as:
  • Maximum iterations
  • Timeout
  • Cancellation
  • External execution limit

Loop Observability

Long-running loops should be observable. Useful information includes:
  • Current iteration
  • Progress
  • Start time
  • Duration
  • Current operation
  • Failure count
  • Retry count
  • Termination reason
This information makes it easier to diagnose workflows that take longer than expected or fail during a particular iteration. The exact observability APIs should follow the BindAI implementation.

Loop Design Pattern

A reliable loop generally follows this pattern:
The important property is that the body changes the state used by the continuation decision. That creates measurable progress toward termination.

Common Use Cases

Loop behavior is useful for:
  • Collection processing
  • Batch processing
  • Pagination
  • Polling external services
  • Repeated validation
  • Incremental synchronization
  • Waiting for state changes
  • Iterative analysis
  • Multi-step record processing
  • Repeated application operations

Best Practices

  • Define a clear termination condition.
  • Make sure the loop can eventually exit.
  • Update the state used by the continuation decision.
  • Keep loop bodies focused.
  • Avoid unnecessary nested loops.
  • Use retry policies for transient execution failures.
  • Consider maximum iteration limits.
  • Add delays or backoff when polling external services.
  • Use timeouts for potentially unbounded execution.
  • Support cancellation for long-running workflows.
  • Monitor progress in production workflows.
  • Test zero, one, multiple, and non-terminating scenarios.
  • Keep business logic separate from orchestration logic.

Current BindAI Scope

BindAI supports loop-based workflow orchestration as part of its workflow capabilities. Loop behavior can be used to coordinate:
  • Repeated agent execution
  • Repeated tool execution
  • Collection processing
  • Batch processing
  • Conditional repetition
  • External polling
  • Iterative validation
  • Multi-step repeated operations
  • Retry and reliability patterns
The exact public API for defining loops should be verified against the installed BindAI workflow implementation. This document intentionally does not assume specific APIs such as:
or a particular predicate signature unless those APIs are verified in the current release.

API Accuracy

The workflow concept described here is iterative execution. Implementation details such as:
  • Loop classes
  • Builder methods
  • Predicate signatures
  • Loop-body registration
  • Exit-path registration
  • Iteration limits
  • Timeout configuration
  • Cancellation behavior
  • Loop serialization
should only be documented as public BindAI APIs when they are present in the implementation and covered by tests. This keeps the documentation synchronized with the actual workflow engine.

Summary

Loops allow BindAI workflows to repeat intentional workflow logic. A typical loop follows:
Loops are useful for collections, batches, pagination, polling, iterative validation, synchronization, and other repeated processes. The loop body should make measurable progress toward termination, while maximum iterations, timeouts, and cancellation can protect against unbounded execution. Loops and retries should remain conceptually separate:
Loops repeat intentional workflow behavior; retries recover from execution failures.
Specific loop APIs should only be documented after they are verified against the current BindAI implementation and tests.